package cn.oracle.eshop.comment.controller;

import cn.oracle.eshop.comment.constant.ShowPictures;
import cn.oracle.eshop.comment.domain.CommentInfoDTO;
import cn.oracle.eshop.comment.domain.CommentInfoVO;
import cn.oracle.eshop.comment.service.CommentInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**评论管理模块的controller组件
 * @author wyh
 * @ClassName CommonentController
 * @date 2022/1/24 /23:04
 */
@RestController
@RequestMapping("/comment")
@Slf4j
public class CommonentController {
    /**评论信息管理模块的service组件*/
    @Autowired
    private CommentInfoService commentInfoService;
    /***
     * 手动发表评论
     * @author wyh
     * @date 2022/1/24
     * @param commentInfoVO 评论中心VO对象
     * @return 处理结果
    */
    @PostMapping("/")
    public Boolean publishComment(CommentInfoVO commentInfoVO, MultipartFile[] files){
        try {
            //为评论设置是否晒图
            Integer showPicture = ShowPictures.NO;
            if (files != null && files.length > 0) {
                for (MultipartFile file : files) {
                    if (file != null) {
                        showPicture = ShowPictures.YES;
                        break;
                    }
                }
            }
            commentInfoVO.setShowPictures(showPicture);
            CommentInfoDTO commentInfodto = commentInfoVO.clone(CommentInfoDTO.class);
            //保存评论信息
            commentInfoService.saveManualPublishedCommentInfo(commentInfodto);
        } catch (Exception e) {
            log.error("error:{}", e);
            return false;
        }
        return true;
    }
}
